package com.wetest.uia2;

import android.app.Instrumentation;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObjectNotFoundException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.googlecode.jsonrpc4j.ErrorResolver;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.wetest.uia2.stub.AutomatorHttpServer;
import com.wetest.uia2.stub.AutomatorService;
import com.wetest.uia2.stub.AutomatorServiceImpl;
import com.wetest.uia2.stub.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.List;

import uiautomator.InstrumentShellWrapper;

/* loaded from: classes7.dex */
public class Main {
    private static final int CUSTOM_ERROR_CODE = -32001;

    public static void main(String... args) {
        try {
            runServer(9008);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static AutomatorServiceImpl automatorServiceImpl;
    public static AutomatorHttpServer automatorHttpServer;

    public static void runServer(int port) throws Exception {
//        InstrumentShellWrapper.getInstance().getContext();
//        UiDevice device = UiDevice.getInstance(InstrumentShellWrapper.getInstance());
//        device.wakeUp();
//        Instrumentation instrumentation = new Instrumentation();
//        UiDevice uiDevice = UiDevice.getInstance(instrumentation);
//        uiDevice.wakeUp();
        automatorServiceImpl = new AutomatorServiceImpl();
        JsonRpcServer jrs = new JsonRpcServer(new ObjectMapper(), automatorServiceImpl, AutomatorService.class);
        jrs.setShouldLogInvocationErrors(true);
        jrs.setErrorResolver(new ErrorResolver() { // from class: com.wetest.uia2.Main.1
            @Override // com.googlecode.jsonrpc4j.ErrorResolver
            public ErrorResolver.JsonError resolveError(Throwable throwable, Method method, List<JsonNode> list) {
                String data = throwable.getMessage();
                if (!throwable.getClass().equals(UiObjectNotFoundException.class)) {
                    throwable.printStackTrace();
                    StringWriter sw = new StringWriter();
                    throwable.printStackTrace(new PrintWriter(sw));
                    data = sw.toString();
                }
                return new ErrorResolver.JsonError(Main.CUSTOM_ERROR_CODE, throwable.getClass().getName(), data);
            }
        });
        AutomatorHttpServer server = new AutomatorHttpServer(port);
        automatorHttpServer = server;
        server.route("/jsonrpc/0", jrs);
        server.start();
        Log.i("server started");
        while (server.isAlive()) {
            Ln.i("[UiAutomator2Server] Starting Server->edit by zjj");
            Thread.sleep(1000L);
        }
    }
}